/*
	DS Kanji Flash
	Copyright (C) 2009,2010 Bernhard Schelling

	This file is part of DS Kanji Flash.

	DS Kanji Flash is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	DS Kanji Flash is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with DS Kanji Flash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "global.h"
#include "dict.h"

static int iFrame;
static const u8 *pStrokes = NULL;

class cSceneShowStrokes : public cScene
{
public:
	cSceneShowStrokes() : cScene(SCN_SHOWSTROKES)
		{ WantsEveryFrame = true; }

	virtual void Enter(void *p1, void *p2, void *p3)
	{
		pStrokes = GetCharStrokes(GetKanjiUCS((int)p1));
		iFrame = 0;
	}

	virtual void ProcessInput()
	{
		if (!ul_keys.pressed.value) return;
		cScene::GoTo(SCN_CARD);
	}
	
	virtual void DrawScreen_Top()
	{
		ulDrawGradientRect(0, 0, 256, 192, RGB15(25, 25, 25), RGB15(25, 25, 25), RGB15(31, 31, 31), RGB15(31, 31, 31));
		ulDrawFillRect((256-128)/2-8, (192-128)/2-8, 256-(256-128)/2+8, 192-(192-128)/2+8, RGB15(0, 0, 0));
		ulDrawFillRect((256-128)/2-6, (192-128)/2-6, 256-(256-128)/2+6, 192-(192-128)/2+6, RGB15(31, 31, 31));
		#define STROKEOFFSETTINGX *128/256+((256-128)/2)
		#define STROKEOFFSETTINGY *128/256+((192-128)/2)
		u8 cNumStrokes;
		for (int iFeather = 1; iFeather >= 0; iFeather--)
		{
			int iDrawnUntil = 0;
			const u8 *pStroke = pStrokes+1;
			for (cNumStrokes = 1; cNumStrokes <= pStrokes[0]; cNumStrokes++)
			{
				u8 cNumPoints = *(pStroke++), px = *(pStroke++)STROKEOFFSETTINGX, py = *(pStroke++)STROKEOFFSETTINGY;
				glDrawCircle(px, py, (!iFeather?5:13), (!iFeather?COL_YELLOW:RGB15(15,15,15)));
				for (int p = 1; p < cNumPoints; p++)
				{
					u8 x = *(pStroke++)STROKEOFFSETTINGX, y = *(pStroke++)STROKEOFFSETTINGY;
					int len = LEN(x,y,px,py)/9; if (len < 1) len = 1; if (len > 8) len = 8;
					if ((iDrawnUntil += len) > iFrame) { x -= (x - px) * (iDrawnUntil - iFrame) / len; y -= (y - py) * (iDrawnUntil - iFrame) / len; }
					glDrawLine(px, py, x, y, (!iFeather?5:13), (!iFeather?5:13), (!iFeather?(p==1?COL_YELLOW:COL_BLACK):RGB15(15,15,15)), (!iFeather?COL_BLACK:RGB15(15,15,15)), 1+iFeather);
					if (iDrawnUntil > iFrame) { break; }
					px = x; py = y;
				}
				if (iDrawnUntil > iFrame) break;
				glDrawCircle(px, py, (!iFeather?5:13), (!iFeather?COL_BLACK:RGB15(15,15,15)));
			}
		}
		char chrStrokeNum[10]; sprintf(chrStrokeNum, "%d.", ulMin(cNumStrokes, pStrokes[0]));
		displayString(fntJapanese, chrStrokeNum, 220, 100);
		iFrame++;
	}
	
	virtual void DrawScreen_Bottom()
	{
		cScene::Get(SCN_CARD)->DrawScreen_Bottom();
	}

} SceneShowStrokes;
